package com.buba.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.service.*;
import common.R;
import entity.sparemanage.*;
import entity.sparemanage.dto.SpareLedgerDto;
import entity.sparemanage.dto.addSpareLedgerDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.buba.service.SpareLedgerService;
import entity.sparemanage.SpareLedger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import utils.PageResult;
import java.util.List;
import org.springframework.web.bind.annotation.RestController;
/**
 * <p>
 * 备件台账 前端控制器
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@RestController
@RequestMapping("/spare-ledger")
public class SpareLedgerController {

    @Autowired
    private SpareLedgerService spareLedgerService;

    @Autowired
    private ImageService imageService;

    @Autowired
    private FileService fileService;

    @Autowired
    private SpareDeviceService spareDeviceService;

    @Autowired
    private SpareTypesService spareTypesService;
    /* *
     * @Author: beisheng
     * @Descriprion: 备件台账列表
     * @Date: 2023/5/19 8:47
     * @Return: common.R
     * @Param: [spareLedger, pagesDto]
     */

    @PostMapping("/selAllSpareLedger")
    public R selAllSpareLedger(@RequestBody SpareLedgerDto spareLedger){
        PageResult allSpareLedger = spareLedgerService.selAllSpareLedger(spareLedger);
        return R.success(allSpareLedger);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 新增备件
     * @Date: 2023/5/23 10:14
     * @Return: common.R
     * @Param: [add]
     */

    @PostMapping("/addSpareLedger")
    public R addSpareLedger(@RequestBody addSpareLedgerDto add){
        spareLedgerService.add(add);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 查看备件台账列表是否已包含当前备件
     * @Date: 2023/5/23 16:13
     * @Return: common.R
     * @Param: [name]
     */

    @GetMapping("/nameSelf/{name}")
    public R nameSelf(@PathVariable("name") String name){
        SpareLedger one = spareLedgerService.getOne(new QueryWrapper<SpareLedger>().eq("delete_flag", 0).eq("sp_name", name));
        if (one!=null){
            return R.success(1);
        }else {
            return R.success(0);
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 备件单查
     * @Date: 2023/5/24 8:51
     * @Return: common.R
     * @Param: [id]
     */

    @GetMapping("/selOneToSpare/{id}")
    public R selOneToSpare(@PathVariable("id") Integer id){
        addSpareLedgerDto spareLedgerDto = spareLedgerService.selOneToSpare(id);
        return R.success(spareLedgerDto);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改备件信息
     * @Date: 2023/5/24 14:06
     * @Return: common.R
     * @Param: [updates]
     */

    @PostMapping("/updateSpareLedger")
    public R updateSpareLedger(@RequestBody addSpareLedgerDto updates){
        spareLedgerService.updateSpareLedger(updates);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 批量删除
     * @Date: 2023/5/24 14:45
     * @Return: common.R
     * @Param: [spIds]
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> spIds){
        for (Integer spId : spIds) {
            SpareLedger spareLedger = new SpareLedger();
            spareLedger.setDeleteFlag(1);
            spareLedger.setSpId(spId);
            try {
                spareLedgerService.updateById(spareLedger);
            }catch (Exception e){
                throw new RuntimeException("删除失败",e);
            }

            File file = new File();
            file.setDeleteFlag(0);
            try {
                fileService.update(file,new QueryWrapper<File>().eq("association_id",spId).eq("file_type",1));
            }catch (Exception e){
                throw new RuntimeException("删除失败",e);
            }

            Image image = new Image();
            image.setDeleteFlag(0);
            try {
                imageService.update(image,new QueryWrapper<Image>().eq("association_id",spId).eq("image_type",1));
            }catch (Exception e){
                throw new RuntimeException("删除失败",e);
            }

        }
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 查询全部备件
     * @Date: 2023/6/14 15:03
     * @Return: common.R
     * @Param: []
     */

    @GetMapping("/getAllSpareLedger")
    public R getAllSpareLedger(){
        List<SpareLedger> list = spareLedgerService.list(new QueryWrapper<SpareLedger>().eq("delete_flag", 0));
        for (SpareLedger spareLedger : list) {
            SpareTypes one = spareTypesService.getOne(new QueryWrapper<SpareTypes>().eq("delete_flag", 0).eq("sp_id", spareLedger.getSpTypeId()));
            spareLedger.setSpTypeName(one.getSpTypename());
        }
        return R.success(list);
    }

    /**
     * @return common.R
     * @Author atlong
     * @Description 查询备件列表信息
     * @Date 2023/6/17 10:46
     * @Param [spareLedger]
     **/
    @PostMapping("/selSpareLedgers")
    public R selSpareLedgers(@RequestBody SpareLedgerDto spareLedger) {
        utils.pageutil.PageResult pageResult = spareLedgerService.selSpareLedgers(spareLedger);
        return R.success(pageResult);
    }

}

